package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.po.Nursecontent;
import com.neuedu.yyzx.po.Nurselevel;
import com.neuedu.yyzx.po.Nurselevelitem;
import com.neuedu.yyzx.service.NursecontentService;
import com.neuedu.yyzx.service.NurselevelitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/nurselevelitem")
public class NurseLevelItemController {
    @Autowired
    private NurselevelitemService nurselevelitemService;

    @Autowired
    private NursecontentService nursecontentService;

    @GetMapping("/list")
    public Ret list() {
        return Ret.ok(nurselevelitemService.list());
    }

    @GetMapping("/page")
    public Ret<List<Nursecontent>> page(Page<Nursecontent> page,Nurselevel nurselevel,String nursingName) {
        // 查询 nurselevelitem 表中符合条件的记录并提取 item_id 列表
        LambdaQueryWrapper<Nurselevelitem> queryWrapper = new LambdaQueryWrapper<>();
        if (nurselevel.getId() != null && nurselevel.getId() != 0) {
            System.out.println(nurselevel.getId());
            queryWrapper.eq(Nurselevelitem::getLevelId, nurselevel.getId());
        }
        List<Integer> itemIds = nurselevelitemService.list(queryWrapper).stream()
                .map(Nurselevelitem::getItemId)
                .collect(Collectors.toList());
        if (itemIds.isEmpty()) {
            return Ret.ok(List.of());
        }
        // 使用 item_id 列表查询 nursecontent 表中的记录
        LambdaQueryWrapper<Nursecontent> queryWrapper2 = new LambdaQueryWrapper<>();
        if(!"".equals(nursingName)){
            queryWrapper2.like(Nursecontent::getNursingName, nursingName);
        }
        page =nursecontentService.page(page, queryWrapper2.in(Nursecontent::getId, itemIds));
        return Ret.ok(page);
    }
    @GetMapping("/getByName/{name}")
    public Ret<Page<Nursecontent>> getByName(@PathVariable String name, Page<Nursecontent> page) {
        LambdaQueryWrapper<Nursecontent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Nursecontent::getNursingName, name);
        queryWrapper.orderByDesc(Nursecontent::getId);
        Page<Nursecontent> resultPage = nursecontentService.page(page, queryWrapper);
        return Ret.ok(resultPage);
    }
    @PostMapping("/saveOrUpdate")
    public Ret<Boolean> saveOrUpdate(@RequestBody Nurselevelitem nurselevelitem) {
        // 设置 is_deleted 字段默认值为 0（未删除）
        nurselevelitem.setIsDeleted(0);
        boolean result = nurselevelitemService.saveOrUpdate(nurselevelitem);
        return Ret.ok(result);
    }

    @DeleteMapping("/{levelId}/{itemId}")
    public Ret<Boolean> removeById(@PathVariable int levelId, @PathVariable int itemId) {
        nurselevelitemService.remove(new LambdaQueryWrapper<Nurselevelitem>()
                .eq(Nurselevelitem::getLevelId, levelId)
                .eq(Nurselevelitem::getItemId, itemId));
        return Ret.ok(nurselevelitemService.removeById(levelId));
    }
}